#!/bin/sh

if [ "$0" == "-h" ]; then
	echo ok the
fi


CC_PROGRAM=gcc


echo    '####################################'
printf '#####  Operating System: '


if [ `uname | grep -i Linux` ]; then
	#Test system
	echo "Linux  ####"
	CC_ARGS="$CC_ARGS -DLINUX -D__LINUX__"
	LINK_ARGS="$LINK_ARGS -export-dynamic "


elif [ `uname | grep -i Darwin` ]; then
	#Test system
	echo "Mac OS X ##"
	CC_ARGS="$CC_ARGS -D__BSDSYSTEM__ -DMACOSX -D__MACOSX__"

elif [ `uname | grep -i FreeBSD` ]; then
	echo "FreeBSD ###"
	CC_ARGS="$CC_ARGS -D__BSDSYSTEM__ -DFREEBSD -D__FREEBSD__"

#I cannot verify the below systems
elif [ `uname | grep -i OpenBSD` ]; then
	echo "OpenBSD ###"
	CC_ARGS="$CC_ARGS -D__BSDSYSTEM__ -DOPENBSD -D__OPENBSD__"

elif [ `uname | grep -i NetBSD` ]; then
	echo "NetBSD ####"
	CC_ARGS="$CC_ARGS -D__BSDSYSTEM__ -DNETBSD -D__NETBSD__"

elif [ `uname | grep -i CYGWIN` ]; then
	echo "Cygwin ####"    
        CC_ARGS="$CC_ARGS -D__BSDSYSTEM__ -DCYGWIN -D__CYGWIN__"

elif [ `uname | grep -i MINGW` ]; then
	echo "Mingw/MSYS  ####"    
        CC_ARGS="$CC_ARGS -D__MINGW__ -DWIN32 -D__WIN32__ -D__WIN32_PTHREADH__"

elif [ `uname | grep -i SunOS` ]; then
	echo "Solaris ###"    
	PATH="/opt/SUNWspro/bin:$PATH"  
	CC_PROGRAM=cc
	OUTPUT="2> /dev/stdout |  grep -v warning"
        CC_ARGS="$CC_ARGS -D__SOLARIS__ -DSOLARIS -D__SUNOS__ -DSUNOS -R/usr/local/lib"

else
	echo "Unix... ###"      
	CC_PROGRAM=cc
        CC_ARGS="$CC_ARGS -DGENUNIX -D__GENUNIX__ "

fi
echo '####################################'

OPENSSL_LINK_ARGS="`pkg-config --libs openssl`"
OPENSSL_CC_ARGS="`pkg-config --cflags openssl`"

LINK_ARGS="$LINK_ARGS -lmp -lm -lpthread"
CC_ARGS="-g $CC_ARGS $1 -DUSE_OPENSSL -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE"


GTK_LINK_ARGS="`pkg-config --libs gtk+-2.0 libglade-2.0`"
GTK_CC_ARGS="`pkg-config --cflags gtk+-2.0 libglade-2.0`"




BUILD_INFO_DIR=".cinfo"
CUR_BUILD_CC_ARGS=""


cleanup(){
echo Cleaning up... ; \
mv *.o $BUILD_INFO_DIR 2> /dev/stdout > /dev/null ; \
rm *~ ; exit ; }


trap cleanup 2


mkdir $BUILD_INFO_DIR 2> /dev/null


#Make sure the arguments haven't changed
echo "$CC_ARGS $OUTPUT" > ./$BUILD_INFO_DIR/args.txt.cur
if [ -r ./$BUILD_INFO_DIR/args.txt.last ]; then
	cmp  ./$BUILD_INFO_DIR/args.txt.last ./$BUILD_INFO_DIR/args.txt.cur > /dev/null || \
	(rm -rd $BUILD_INFO_DIR ; echo Different Build Arguments, rebuilding all...)
fi


cp $0 ./$BUILD_INFO_DIR/build.txt.cur 2> /dev/null
if [ -r ./$BUILD_INFO_DIR/build.txt.last ]; then
	cmp  ./$BUILD_INFO_DIR/build.txt.last ./$BUILD_INFO_DIR/build.txt.cur > /dev/null || \
	(rm -rd $BUILD_INFO_DIR ; echo The Build Script Has Changed, rebuilding all...)
fi


mkdir $BUILD_INFO_DIR 2> /dev/null

#comparision for checking if arguments have changed
echo "$CC_ARGS $OUTPUT" > ./$BUILD_INFO_DIR/args.txt.last

#Comparision for checking if build file has changed
cp $0 ./$BUILD_INFO_DIR/build.txt.last



echo Compiling...
echo 


mv  ./$BUILD_INFO_DIR/*.o .  2> /dev/null

BOLD=`tput bold`
NORM=`tput sgr0`

build_object(){ 
	      if [ "`stat $CUR_STAT_FILE 2> /dev/null`" ]; then 
	       stat $CUR_STAT_FILE | grep -v 'Access' > ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.cur ; \
              cmp  ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.last ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.cur 2> /dev/null > /dev/null || \
              (echo -e $BOLD$CUR_BUILD_FILE$NORM && ($CC_PROGRAM $CC_ARGS $CUR_BUILD_CC_ARGS -c $CUR_BUILD_FILE $OUTPUT && stat $CUR_STAT_FILE | grep -v 'Access' > ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.last)) ; 
	      else
		echo $CUR_BUILD_FILE \(not found\)
	      fi ; }

CUR_BUILD_FILE="cgi.c"             ; CUR_STAT_FILE="cgi.c cgi.h"                         ; build_object
CUR_BUILD_FILE="lang.c"            ; CUR_STAT_FILE="lang.c lang.h"                       ; build_object
CUR_BUILD_FILE="env.c"             ; CUR_STAT_FILE="env.c env.h"                         ; build_object
CUR_BUILD_FILE="encode.c"          ; CUR_STAT_FILE="encode.c encode.h"                   ; build_object
CUR_BUILD_FILE="file.c"            ; CUR_STAT_FILE="file.c file.h"                       ; build_object
CUR_BUILD_FILE="home.c"            ; CUR_STAT_FILE="home.c home.h	"                ; build_object
CUR_BUILD_FILE="mini_db.c"         ; CUR_STAT_FILE="mini_db.c mini_db.h"                 ; build_object
CUR_BUILD_FILE="multimedia_info.c" ; CUR_STAT_FILE="multimedia_info.c multimedia_info.h" ; build_object
CUR_BUILD_FILE="sha1.c"            ; CUR_STAT_FILE="sha1.c sha1.h"                       ; build_object
CUR_BUILD_FILE="string.c"          ; CUR_STAT_FILE="string.c string.h"                   ; build_object
CUR_BUILD_FILE="strtok_r.c"        ; CUR_STAT_FILE="strtok_r.c "                         ; build_object
CUR_BUILD_FILE="spp.c"             ; CUR_STAT_FILE="spp.c "                              ; build_object
CUR_BUILD_FILE="time.c"            ; CUR_STAT_FILE="time.c time.h"                       ; build_object
CUR_BUILD_FILE="smtp.c"            ; CUR_STAT_FILE="smtp.c smtp.h"                       ; build_object
CUR_BUILD_FILE="http.c"            ; CUR_STAT_FILE="http.c http.h"                       ; build_object
CUR_BUILD_FILE="math.c"            ; CUR_STAT_FILE="math.c math.h"                       ; build_object
CUR_BUILD_FILE="treehash.c"        ; CUR_STAT_FILE="treehash.c treehash.h"               ; build_object
CUR_BUILD_FILE="pop3.c"            ; CUR_STAT_FILE="pop3.c pop3.h"                       ; build_object
CUR_BUILD_FILE="md4.c"             ; CUR_STAT_FILE="md4.c md4.h"                         ; build_object
CUR_BUILD_FILE="md5.c"             ; CUR_STAT_FILE="md5.c md5.h"                         ; build_object
CUR_BUILD_FILE="xdirent.c"         ; CUR_STAT_FILE="xdirent.c xdirent.h"                 ; build_object
CUR_BUILD_FILE="gtk_icon.c"        ; CUR_STAT_FILE="gtk_icon.c gtk_icon.h"               ; CUR_BUILD_CC_ARGS="$GTK_CC_ARGS"     ; build_object ; CUR_BUILD_CC_ARGS=""
CUR_BUILD_FILE="portable_socket.c" ; CUR_STAT_FILE="portable_socket.c portable_socket.h" ; CUR_BUILD_CC_ARGS="$OPENSSL_CC_ARGS" ; build_object ; CUR_BUILD_CC_ARGS=""
CUR_BUILD_FILE="mkcert.c"          ; CUR_STAT_FILE="mkcert.c"                            ; CUR_BUILD_CC_ARGS="$OPENSSL_CC_ARGS" ; build_object ; CUR_BUILD_CC_ARGS=""



rm ./$BUILD_INFO_DIR/*.cur






echo "Finished compiling object files!"
echo

echo "Compiling/Linking...."
echo



# build_program(){ stat $CUR_BUILD_FILE   | grep -v 'Access' > ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.cur ; \
#               cmp  ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.last ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.cur 2> /dev/null > /dev/null || \
#               (echo Compiling $CUR_BUILD_FILE as $CUR_OUTPUT_FILE && ($CC_PROGRAM $CC_ARGS $LINK_ARGS $CUR_BUILD_CC_ARGS -o a.out $CUR_BUILD_FILE $OUTPUT && stat $CUR_BUILD_FILE | grep -v 'Access' > ./$BUILD_INFO_DIR/$CUR_BUILD_FILE.last && mv a.out $CUR_OUTPUT_FILE)) ; }

# CUR_BUILD_FILE="test.c"         ; CUR_OUTPUT_FILE="test.exe"              ; CUR_BUILD_CC_ARGS="$OPENSSL_CC_ARGS $OPENSSL_LINK_ARGS *.o"                             ; build_program ; CUR_BUILD_CC_ARGS="" 
# CUR_BUILD_FILE="server.c"       ; CUR_OUTPUT_FILE="server.exe"            ; CUR_BUILD_CC_ARGS="$GTK_CC_ARGS $GTK_LINK_ARGS $OPENSSL_CC_ARGS $OPENSSL_LINK_ARGS *.o" ; build_program ; CUR_BUILD_CC_ARGS="" 
# CUR_BUILD_FILE="client.c"       ; CUR_OUTPUT_FILE="client.exe"            ; CUR_BUILD_CC_ARGS="$GTK_CC_ARGS $GTK_LINK_ARGS $OPENSSL_CC_ARGS $OPENSSL_LINK_ARGS *.o" ; build_program ; CUR_BUILD_CC_ARGS="" 
# CUR_BUILD_FILE="crash_agent.c"  ; CUR_OUTPUT_FILE="server_crashagent.exe" ; CUR_BUILD_CC_ARGS="$GTK_CC_ARGS $GTK_LINK_ARGS $OPENSSL_CC_ARGS $OPENSSL_LINK_ARGS *.o" ; build_program ; CUR_BUILD_CC_ARGS="" 


$CC_PROGRAM -o treehash_test.exe    -g       treehash_test.c  *.o $GTK_CC_ARGS $GTK_LINK_ARGS $CC_ARGS $LINK_ARGS $OPENSSL_CC_ARGS $OPENSSL_LINK_ARGS    
$CC_PROGRAM -o antagonist.exe    -g       antagonist.c  *.o $GTK_CC_ARGS $GTK_LINK_ARGS $CC_ARGS $LINK_ARGS $OPENSSL_CC_ARGS $OPENSSL_LINK_ARGS    



echo
cleanup



